<template>
  <div class="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <div class="title-bar">
          <img src="../assets/vue.svg" alt="logo" class="logo">
          <div class="title">用户中心</div>
        </div>
      </a-col>
      <a-col flex="auto">
        <a-menu
            v-model:selectedKeys="current"
            mode="horizontal"
            :items="items"
            @click="doMenuClick"
        />
      </a-col>
      <a-col flex="100px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            {{loginUserStore.loginUser.username}}
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登陆</a-button>
          </div>
        </div>
      </a-col>
    </a-row>


  </div>
</template>
<script lang="ts" setup>
import {h, ref} from 'vue';
import {HomeOutlined, CrownOutlined} from '@ant-design/icons-vue';
import {type MenuProps} from 'ant-design-vue';
import {useRouter} from "vue-router";
import {useLoginUserStore} from "@/store/useLoginUserStore.ts";

const loginUserStore = useLoginUserStore();

const router = useRouter();
//点击菜单后的路由跳转
const doMenuClick = ({key}:{key:string}) =>{
  router.push({
    path: key,
  })
}
//切换路由后更新选中按钮
router.afterEach((to) => {
  current.value = [to.path];
})

const current = ref<string[]>(['mail']);
const items = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/user/login',
    label: '用户登陆',
    title: '用户登陆',
  },
  {
    key: '/user/register',
    label: '用户注册',
    title: '用户注册',
  },
  {
    key: '/admin/userManage',
    icon: () => h(CrownOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: 'other',
    label: h(
        'a',
        {href: 'https://www.codefather.cn', target: '_blank'},
        '编程导航'
    ),
    title: '编程导航',
  },
]);
</script>
<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 18px;
}
</style>
